package com.poetize.plus.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.poetize.plus.entity.dto.ArticleDTO;
import com.poetize.plus.entity.dto.page.ArticlePageDTO;
import com.poetize.plus.entity.vo.page.ArticlePageVO;
import com.poetize.plus.entity.vo.ArticleVO;
import com.poetize.plus.entity.vo.SelectVO;
import com.poetize.plus.service.ArticleService;
import com.poetize.plus.utils.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author zuosy
 * @Date 2024/3/30 14:39
 **/
@Tag(name = "文章信息")
@RestController
@RequestMapping("/admin/article")
@RequiredArgsConstructor
public class ArticleController {

    private final ArticleService articleService;

    @GetMapping("/articlePage")
    @Operation(summary = "文章分页数据")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Page<ArticlePageVO>> articlePage(ArticlePageDTO articlePageDTO){
        return R.ok(articleService.articlePage(articlePageDTO));
    }

    @PostMapping("/addArticle")
    @Operation(summary = "新增文章信息")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> addArticle(@RequestBody @Validated(Insert.class) ArticleDTO articleDTO){
        return R.ok(articleService.addArticle(articleDTO));
    }

    @GetMapping("/{id}")
    @Operation(summary = "文章详情")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<ArticleVO> queryArticle(@PathVariable("id") Long id){
        return R.ok(articleService.queryArticle(id));
    }

    @PostMapping("/updateArticle")
    @Operation(summary = "修改文章信息")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> updateArticle(@RequestBody @Validated(Update.class) ArticleDTO articleDTO){
        return R.ok(articleService.updateArticle(articleDTO));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除文章信息")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> deleteArticle(@PathVariable("id") Long id){
        return R.ok(articleService.deleteArticle(id));
    }

    @GetMapping("/articleSelect")
    @Operation(summary = "文章下拉框")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<List<SelectVO>> articleSelect(@RequestParam(name = "title") String title){
        return R.ok(articleService.getArticleSelect(title));
    }

    @GetMapping("/changeViewStatus/{id}/{status}")
    @Operation(summary = "修改是否可见状态")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> changeViewStatus(@PathVariable("id") Long id,
                                       @PathVariable("status") Boolean status){
        return R.ok(articleService.changeViewStatus(id, status));
    }

    @GetMapping("/changeCommentStatus/{id}/{status}")
    @Operation(summary = "修改是否开启评论状态")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> changeCommentStatus(@PathVariable("id") Long id,
                                       @PathVariable("status") Boolean status){
        return R.ok(articleService.changeCommentStatus(id, status));
    }

    @GetMapping("/changeRecommendStatus/{id}/{status}")
    @Operation(summary = "修改是否推荐状态")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> changeRecommendStatus(@PathVariable("id") Long id,
                                          @PathVariable("status") Boolean status){
        return R.ok(articleService.changeRecommendStatus(id, status));
    }
}
